package restlet;

import java.util.Vector;

import model.Mesto;
import model.PutnLinija;
import model.PutnaDuz;
import model.Util;

import org.restlet.data.Reference;
import org.restlet.resource.ServerResource;

import restlet.interfaces.TripLineResourceI;
import beans.TripLine;

public class TripLineResource extends ServerResource implements
		TripLineResourceI {
	@Override
	public Vector<TripLine> allTripLines() {

		System.out.println("TripLineResource, pristiglo: " + getQuery());
		
		String sMestoOd = Reference.decode(
				(String) getRequest().getAttributes().get("mestoOd"));
		String sMestoDo = Reference.decode(
				(String) getRequest().getAttributes().get("mestoDo"));
		/*String sPovlasticaId = Reference.decode(
				(String) getRequest().getAttributes().get("povlasticaId"));*/

		if (sMestoOd == null || sMestoDo == null) {
			return null;
		}

		Vector<PutnaDuz> allTrips = Util.getAllEntitiesFromTable(PutnaDuz.class);

		Mesto mMestoOd = Util.getMestoFromName(sMestoOd);
		Mesto mMestoDo = Util.getMestoFromName(sMestoDo);
		Vector<PutnLinija> rez = Util.getPaths(mMestoOd, mMestoDo, allTrips,Util.NAJBRZI);

		Vector<TripLine> retVal = new Vector<TripLine>();
		for (PutnLinija pl : rez) {
			System.out.println("Size: " + pl.getPutneDuzi().size());
			retVal.add(pl.toTripLine());
		}

		// TODO ovde treba staviti logiku da se uracunaju sve cene sa
		// povlasticom
		// i da se rezultat vrati u rastucem redosledu po ukupnoj ceni

		System.out.println("Ucitane iz baze");
		return retVal;
	}
}
